package com.mavenq.fly.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/cache")
public class LocalCacheController {


    public static LoadingCache<String, JSONObject> loadingCache = CacheBuilder.newBuilder()
            .expireAfterWrite(7, TimeUnit.DAYS)
            .maximumSize(100)
            .build(new CacheLoader<String, JSONObject>() {
                @Override
                public JSONObject load(String key) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("name", "afei");
                    return jsonObject;
                }
            });


    @RequestMapping(value = "get")
    public JSONObject getCacheData() throws ExecutionException {
        return loadingCache.get("name");
    }

    public static void main(String[] args) throws ExecutionException {
        System.out.println(loadingCache.get("1"));
    }

}
